package com.fkg.test.picselect;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.fkg.test.MainActivity;
import com.fkg.test.R;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by FanKaige on 2017/11/13.
 */

public class picselect extends Activity implements View.OnClickListener{

    Button btn_add;
    RecyclerView recyclerView;
    picAdapter picAdapter;
    List<LocalMedia> selectList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pic_select);

        btn_add = (Button) findViewById(R.id.btn_add);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        recyclerView.setLayoutManager(new LinearLayoutManager(picselect.this));
        if (picAdapter == null) {
            picAdapter = new picAdapter(selectList);
        }
        recyclerView.setAdapter(picAdapter);

        btn_add.setOnClickListener(this);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);

                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    picAdapter.setList(selectList);
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add:
                PictureSelector.create(picselect.this)
                        .openGallery(PictureMimeType.ofImage())
                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
        }
    }

    public class picAdapter extends RecyclerView.Adapter<picAdapter.viewHolder>{

        List<LocalMedia> pics = new ArrayList<>();

        public picAdapter(List<LocalMedia> pics) {
            this.pics = pics;
        }
        public void setList(List<LocalMedia> pics) {
            this.pics = pics;
            this.notifyDataSetChanged();
        }

        @Override
        public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(picselect.this,R.layout.pic_item,null);
            return new viewHolder(view);
        }

        @Override
        public void onBindViewHolder(viewHolder holder, int position) {

            Glide.with(picselect.this).load(pics.get(position).getPath()).into(holder.imageView);

        }

        @Override
        public int getItemCount() {
            return pics.size();
        }

        public class viewHolder extends RecyclerView.ViewHolder{

            ImageView imageView;

            public viewHolder(View itemView) {
                super(itemView);

                imageView = (ImageView) itemView.findViewById(R.id.pic_item_img);
            }
        }
    }
}
